package com.leezk.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leezk.order.entity.Menu;
import com.leezk.order.entity.TagMapper;
import com.leezk.order.exception.NoDataException;
import com.leezk.order.mapper.MenuMapper;
import com.leezk.order.mapper.TagMapperMapper;
import com.leezk.order.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author leotong
 * @since 2022-03-16
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private TagMapperMapper tagMapperMapper;

    @Override
    public Page<Menu> getMenus(Page<Menu> page) {
        return menuMapper.selectPage(page,null);
    }

    @Override
    public List<Menu> getAddMenu(int id,String typeCode) {
        List<Menu> menus = menuMapper.selectList(null);
        //已经使用过了的菜单集合
        List<String> midsList = tagMapperMapper.getTagMapperByTypeCode(typeCode);
        //可以使用的菜单集合
        TagMapper tagMapper = tagMapperMapper.selectById(id);

        List<String> midList=new ArrayList<>();
        for (String mids : midsList) {
            if (mids!=null){
                String[] split = mids.split(",");
                midList.addAll(Arrays.asList(split));
            }
        }
        String mid = tagMapper.getMid();
        if (mid!=null){
            String[] split = mid.split(",");
            for (String s : split) {
                midList.remove(s);
            }
        }

        Stream<Menu> menuStream = menus.stream().filter(menu -> !midList.contains(menu.getId().toString()));
        List<Menu> collect = menuStream.collect(Collectors.toList());
        if (collect.size()==0){
            throw new NoDataException("当前推荐清单咩有可操作的菜单");
        }
        return collect;
    }

    @Override
    public List<Menu> getMenuByDic(int did) {
        if (did==-1){
            return this.list();
        }
        if (did==0){
            return null;
        }
        QueryWrapper<TagMapper> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("did",did);
        TagMapper tagMapper = tagMapperMapper.selectOne(queryWrapper);
        String mids = tagMapper.getMid();
        List<Menu> menus=new ArrayList<>();
        if (mids!=null&& !mids.equals("")){
            String[] split = mids.split(",");
            for (String s : split) {
                Menu menu = menuMapper.selectById(s);
                if (menu!=null){
                    menus.add(menu);
                }
            }
        }
        return menus;
    }

    @Override
    public List<Menu> getMenusByName(String name) {
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",name);
        return this.list(queryWrapper);
    }
}
